package views;

import controllers.Permission;
import controllers.User;
import db.PermissionDAO;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;

import javax.swing.*;
import db.UserDAO;

public class AddUserFrame extends JFrame {
	
	private static final int WIDTH=250;
	private static final int HEIGHT=300;
	private static final String TITLE = "Add User";
	private JLabel usernameLabel, passwordLabel, permissionLabel;
        private JComboBox permission;
	private JTextField username, password;
	private JButton save;
        private List<Permission> permissions;
	
	public AddUserFrame(int xBound, int yBound){
		this.setTitle(TITLE);
		this.setBounds(xBound+100, yBound+100, WIDTH, HEIGHT);
		this.setResizable(false);
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		
		Container c = this.getContentPane();
		
		KeyListener l = new KeyListener(){
			@Override
			public void keyPressed(KeyEvent arg0) {
				enableSave();
			}
			@Override
			public void keyReleased(KeyEvent arg0) {
				enableSave();	
			}
			@Override
			public void keyTyped(KeyEvent arg0) {
				enableSave();	
			}
		};
		
		usernameLabel = new JLabel("Username");
		usernameLabel.setBounds(25, 25, 200, 25);
		panel.add(usernameLabel);
		
		username = new JTextField();
		username.setBounds(25, 50, 200, 25);
		username.addKeyListener(l);
		panel.add(username);
		
		passwordLabel = new JLabel("Password");
		passwordLabel.setBounds(25, 75, 200, 25);
		panel.add(passwordLabel);
		
		password = new JTextField();
		password.setBounds(25, 100, 200, 25);
		password.addKeyListener(l);
		panel.add(password);
		
		permissionLabel = new JLabel("Permission");
		permissionLabel.setBounds(25, 125, 200, 25);
		panel.add(permissionLabel);
		
		permission = new JComboBox();
		permission.setBounds(25, 150, 200, 25);
                permissions = getPermissions();
		setPermissionList(permissions);
		permission.addKeyListener(l);
		panel.add(permission);
		
		save = new JButton("Save");
		save.setBounds(25, 200, 100, 30);
		save.setEnabled(false);
		save.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				User u = new User();
                                u.setUsername(username.getText());
				u.setPassword(password.getText());
				u.setPermissionLevelID(permissions.get(permission.getSelectedIndex()).getId());
				UserDAO.addUser(u);
				//List<User> users = UserDAO.getUsers();
				ViewUsersFrame.updateRows();
				AddUserFrame.this.dispose();
			}
		});
		panel.add(save);
		
		c.add(panel);
		
		this.setVisible(true);
		
	}
	
	public void enableSave(){
		if(username.getText().length()>0&&password.getText().length()>0&&permissions.get(permission.getSelectedIndex()).getId()>0){
			save.setEnabled(true);
		}
		else{
			save.setEnabled(false);
		}
	}

        public static List<Permission> getPermissions(){
		return PermissionDAO.getPermissions();
	}

	public  void setPermissionList(List<Permission> list){
		for(int i=0; i<list.size();i++){
			permission.addItem(list.get(i));
		}
	}
}
